package com.smrs.deliver.service;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jof.framework.dao.hibernate.BaseDAOHibernateImpl;
import com.jof.framework.util.Pager;
import com.smrs.basicdata.dao.StoreDao;
import com.smrs.basicdata.model.StoreModel;
import com.smrs.deliver.dao.DeliverDetailDao;
import com.smrs.deliver.dao.DeliverHeaderDao;
import com.smrs.deliver.model.DeliverDetailModel;
import com.smrs.deliver.model.DeliverHeaderModel;
import com.smrs.deliver.model.DeliverHeaderSearchModel;
import com.smrs.report.model.ReportMonthlyDeliverModel;
import com.smrs.report.model.ReportSearchModel;
import com.smrs.service.BaseService;

@Component(value="deliverHeaderService")
public class DeliverHeaderService extends BaseService<DeliverHeaderModel,Long>{
	@Autowired
	private DeliverHeaderDao deliverHeaderDao;
	
	@Autowired
	private DeliverDetailDao deliverDetailDao;
	@Autowired
	private StoreDao storeDao;
	public Pager<DeliverHeaderModel> getByNameLikePager(String name, Pager<DeliverHeaderModel> pager){
		return deliverHeaderDao.getByNameLikePager(name,pager);
		
	}

	public Pager<DeliverHeaderModel> findDeliverHeader(DeliverHeaderSearchModel deliverHeaderSearch, Pager<DeliverHeaderModel> pager){
		
		Pager<DeliverHeaderModel> tempPager = deliverHeaderDao.findDeliverHeader(deliverHeaderSearch, pager);
		return tempPager;
	}
	
	public void updateCalculateDeliverHeaderQuantifyAndAmount(DeliverHeaderModel deliverHeader){
		List<DeliverDetailModel> list=deliverDetailDao.findByDeliverHeader(deliverHeader);
		if(CollectionUtils.isNotEmpty(list)){
			DeliverHeaderModel headerModel = this.getByPK(deliverHeader.getId());
			Double totalQuantify =0d;
			Double totalAmount = 0d;
			for(DeliverDetailModel oneDetail : list){
				if(oneDetail.getActualQuantify()!=null){
					totalQuantify = totalQuantify+oneDetail.getActualQuantify();
				}
				if(oneDetail.getActualAmount()!=null){
					totalAmount = totalAmount +oneDetail.getActualAmount();
				}
			}
			headerModel.setActualQuantify(totalQuantify);
			headerModel.setActualAmount(totalAmount);
			this.updateModel(headerModel);
		}
	}
	
	/**
	 * 每月销售报表
	 * @param searchModel
	 * @param pager
	 * @return
	 */
	public Pager<ReportMonthlyDeliverModel> getDeliverReportOfMonthly(ReportSearchModel searchModel,Pager<ReportMonthlyDeliverModel> pager){
		Pager<ReportMonthlyDeliverModel> tempPager =  deliverHeaderDao.getDeliverReportOfMonthly(searchModel, pager);
		List<ReportMonthlyDeliverModel> list = tempPager.getRecords();
		if(CollectionUtils.isNotEmpty(list)){
			HashMap<Long,StoreModel> storeMap = new HashMap<Long,StoreModel>();
			for(ReportMonthlyDeliverModel report:list){
				Long storeId=report.getStoreId();
				StoreModel storeModel = storeMap.get(storeId);
				if(storeModel==null){
				 storeModel=storeDao.get(storeId);
				 storeMap.put(storeId, storeModel);
				}
				report.setSaleAmount(report.getActualAmount());
				report.setInvoiceAmount(report.getActualAmount() * (1-storeModel.getRebateRate()));
			}
		}
		return tempPager;
	}
	
	@Override
	public BaseDAOHibernateImpl<DeliverHeaderModel, Long> getPerformDao() {		
		return deliverHeaderDao;
	}

	
	
}
